package com.lam.dormitory.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lam.dormitory.entity.Dormitory;
import com.lam.dormitory.entity.Repair;
import com.lam.dormitory.mapper.DormitoryMapper;
import com.lam.dormitory.mapper.RepairMapper;
import com.lam.dormitory.service.IRepairService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lam.dormitory.util.ResponseTemplate;
import com.lam.dormitory.vo.RepairVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author lam
 * @since 2019-12-26
 */
@Service
public class RepairServiceImpl extends ServiceImpl<RepairMapper, Repair> implements IRepairService {
    @Autowired
    private DormitoryMapper dormitoryMapper;
    @Autowired
    private RepairMapper repairMapper;

    @Override
    public List<RepairVo> selectRepairVoByDorId(Long dorId) {
        return this.baseMapper.selectRepairVoByDorId(dorId);
    }

    @Override
    public List<RepairVo> selectRepairVo(Repair repair) {
        return this.baseMapper.selectRepairVo(repair);
    }

    @Override
    public ResponseTemplate saveBatch(List<RepairVo> list) {
        List<Repair> repairList = new ArrayList<>();
        for (RepairVo repairVo : list) {
            QueryWrapper<Dormitory> wrapper = new QueryWrapper<>();
            wrapper.lambda().eq(Dormitory::getBuildingNo, repairVo.getBuildingNo()).eq(Dormitory::getDorNo, repairVo.getDorNo());
            Dormitory dormitoryOne = dormitoryMapper.selectOne(wrapper);

            QueryWrapper<Repair> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(Repair::getRepairId, repairVo.getRepairId());
            Repair repairOne = repairMapper.selectOne(queryWrapper);
            if (repairOne == null) {
                Repair repair = new Repair();
                repair.setRepairId(repairVo.getRepairId());
                repair.setDorId(dormitoryOne.getDorId());
                repair.setRepairKind(repairVo.getRepairKind());
                repair.setRepairReason(repairVo.getRepairReason());
                repair.setSubmitDate(repairVo.getSubmitDate());
                repair.setResolvedDate(repairVo.getResolvedDate());
                repair.setStatus(repairVo.getStatus());
                repairList.add(repair);
            }
        }
        boolean b = this.saveBatch(repairList);
        if (b) {
            return new ResponseTemplate(200, "添加成功");
        }
        return new ResponseTemplate(500, "添加失败");
    }


}
